﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@page import="global.Constants"%>
<%@page import="com.aiflow.Action.DictionaryAction"%>
<%@page import="com.aiflow.Action.DepartmentAction"%>
<%@ page import="com.aiflow.bean.User"%>
<html>
<head>
	<%
		String baseUrl = request.getContextPath();
		User use = (User)session.getAttribute("user");
	%>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type="text/javascript">
	    //这里的全局是为了修改之后的刷新
	    var personConfListGrid;
		$(document).ready(function(){
		    var sortPersonWindow;
		    //角色勾选的变量
		    var checkid;
		    var checktext;
		    //部门的变量
		    var deptid = '<%=use.getDept_id()%>';
		    var depttext;
			// 全局参数
			var baseParams = {limit:<%=Constants.PAGE_SIZE %>,start : 0};
			var EXT_QUERY_TIME_OUT = 1200000;
			// 传报数据源  userName,u.telep ,d.name as targetName
		    var personConfListStore = new Ext.data.JsonStore({
		        url: '${pageContext.request.contextPath}/findPersonConfig.action',
		        root: 'datas',
				timeout : EXT_QUERY_TIME_OUT,
		        totalProperty: 'results',
		        fields: ['id','userName','telep','targetName','superviseTarget','dept_name','spec'],
		        baseParams: baseParams,
		        autoLoad: true,
				listeners : {  
          load : function() {  
              isDetailReady = true;// 查询置换标志为已查过   
            }  
            }  
		    });
		    
		    var form = new Ext.form.FormPanel({ 
			  isUpload:true, //表示文件上传 
			  items:[{ 
			    xtype:'textfield', 
			    inputType : 'file' //文件组件 
			  }] 
			})
		
			// 工具栏
		    var personConfListToolbar = new Ext.Toolbar({
		    	renderTo: 'personConfListToolBarDiv',
		    	items: [
		    	    new Ext.Button({
					    id: 'personConf-export-button',
					    text: '督办人员导出',
						iconCls: 'page_white_excel'
					}),'-',
					new Ext.Button({
					    id: 'personConfList-add-button',
					    text: '添加',
						iconCls: 'user_add'
						//disabled:true
					}),
					new Ext.Button({
					    id: 'personConfList-update-button',
					    text: '编辑',
						iconCls: 'user_edit'
					}),
					new Ext.Button({
					    id: 'personConfList-delete-button',
					    text: '删除',
						iconCls: 'status_busy'
					}),
					'-',
					new Ext.Button({
					    id: 'personConfList-sort-button',
					    text: '督办人员排序',
						iconCls: 'hide'
					}),
					'-',
		            new Ext.ux.form.SearchField({
		                store: personConfListStore,
		                width: 320,
		                id:'userName',
		                paramName: 'userName',
		                emptyText: '请输入督办人员名称',
		                style: 'margin-left: 10px;'
		            }),'-',
		            new Ext.Button({
					    id: 'person-upload-button',
					    text: 'Excel导入',
						iconCls: 'status_busy'
					}),
					new Ext.Button({
					    id: 'template-download-button',
					    text: '模板下载',
						iconCls: 'status_busy'
					})
				]
			});
 
			// 数据表格    dept_name spec
			var sm = new Ext.grid.CheckboxSelectionModel();
			personConfListGrid = new Ext.grid.GridPanel({
				id: 'personConfListGrid',
				renderTo: 'personConfListGridDiv',
				border: false,
				stateful: true,
			    autoScroll: 'auto',
		        store: personConfListStore,
		        loadMask: true,
		        cm: new Ext.grid.ColumnModel({
		            defaults: {
		                width: 100,
		                sortable: true
		            },
			        columns: [
						sm,
						new Ext.grid.RowNumberer({header:'№',width:50}),
						{id:'id',header: '姓名', width: 150, hidden: true, sortable: true, dataIndex: 'id'},
						{id:'userName',header: '姓名', width: 150, hidden: false, sortable: true, dataIndex: 'userName'},
			            {id:'telep',header: '电话', width: 100,hidden: false, sortable: true, dataIndex: 'telep'},
			            {id:'targetName',header: '督办对象', width: 200,hidden: false, sortable: true, dataIndex: 'targetName'},
			            {id:'superviseTarget',header: '督办对象编号', width: 100,hidden: true, sortable: true, dataIndex: 'superviseTarget'},
			            {id:'dept_name',header: '部门', width: 200,hidden: false, sortable: true, dataIndex: 'dept_name'},
			            {id:'spec',header: '专业', width: 200,hidden: false, sortable: true, dataIndex: 'spec'}
			            ]
		        }),
		        sm: sm,
		        columnLines: true,
		         bbar: new Ext.PagingToolbar({
					pageSize: <%=Constants.PAGE_SIZE %>,
					store: personConfListStore,
					displayInfo: true,
					displayMsg: Anynote.PAGINGTOOLBAR_DISPLAYMSG,
					emptyMsg: Anynote.PAGINGTOOLBAR_EMPTYMSG,
					doLoad: function(start){
						baseParams.start = start;
						this.store.load({params: baseParams});
					}
		        })
		      
		    });
			// 设置Grid高度和宽度
			 Anynote.resizeGridTo("personConfListGrid", 0, 56);
		
             $("#personConfList-add-button").click(function(){
			  	// 传报对象 数据源
	        var alarmStore = new Ext.data.ArrayStore({  
	            fields : ['alarm_id', 'alarm_name'],  
	            data : <%=DictionaryAction.getDictionaryCombo("748")%>,
	            sortInfo: {field: "alarm_id", direction: "ASC"}
	        });
	        
	         // 区域  数据源  getDepartmentCombo
	         var regionStoreper = new Ext.data.ArrayStore({  
	            fields : ['region_id', 'region_name'],  
	            data : <%=DepartmentAction.getDepartmentCombods(use.getDept_id())%>,
	            sortInfo: {field: "region_id", direction: "ASC"}
	        });
	        
	           var specialtyStore = new Ext.data.ArrayStore({  
	            fields : ['specialty_id', 'specialty_name'],  
	            data : <%=DictionaryAction.getDictionaryCombo("98")%>,
	            sortInfo: {field: "specialty_id", direction: "ASC"}
	        });
			 
		     	// 新增人员设置Form
				var addPersonConfFormPanel = new Ext.FormPanel({
					id: 'addPersonConfFormPanel',
			        labelWidth: 60,
			        buttonAlign: 'center',
			        border: false,
			        bodyStyle: 'padding:10px;text-align:left;background-color:transparent;',
			        url: '',
		            items:[{//   人员姓名
			                xtype:'textfield',
			                id:'user_name',
			                name: 'user_name',
			                fieldLabel: '姓名',
			                anchor:'98%',
			                allowBlank:false,
			                maxLength: 20
			           },{//    手机号
			                xtype:'textfield',
			                name: 'telep',
			                fieldLabel: '手机号码',
			                anchor:'98%',
			            //  regex: /^1[\d]{10}$/,
                            regexText: '手机号码必须1开头,后面跟10位数字！',
                            allowBlank:false,
			                maxLength: 20
			           },{  
			                xtype:'combo',
			                hiddenName: 'superviseTarget',
			                fieldLabel: '督办对象',
			                store: alarmStore,
			                mode : 'local',
			                triggerAction: "all",
			                valueField: 'alarm_id',
			                displayField: 'alarm_name',
			                allowBlank:false,
			                editable: false,
			                width: 230,
			                anchor:'98%'
			           },{  
			                xtype:'combo',
			                id:'region',
			                hiddenName: 'region',
			                fieldLabel: '所属省市',
			                store: regionStoreper,
			                mode : 'local',
			                triggerAction: "all",
			                valueField: 'region_id',
			                displayField: 'region_name',
			                allowBlank:false,
			                editable: false,
			                width: 230,
			                anchor:'98%'
			           },{  
			                xtype:'combo',
			                hiddenName: 'specialty',
			                fieldLabel: '专业',
			                store: specialtyStore,
			                mode : 'local',
			                triggerAction: "all",
			                valueField: 'specialty_id',
			                displayField: 'specialty_name',
			                allowBlank:false,
			                editable: false,
			                width: 230,
			                anchor:'98%'
			           },{//   描叙
			                xtype:'textarea',
			                id: 'describe',
			                name: 'describe',
			                fieldLabel:'描叙',
			                width: 230,
			                anchor:'98%'
			              
		                }],
			        buttons: [{
	                    text:'提交',
	                    handler: function(){
				        	var form = addPersonConfFormPanel.getForm();
						    if(form.isValid()){
						     //    form.findField('rolename').setValue(checktext.toString());
						    	// 发送请求
								Anynote.ajaxRequest({
									baseParams: form.getValues(),
									action: '${pageContext.request.contextPath}/addPersonConfig.action',
									callback: function(jsonResult){
										personConfListGrid.getStore().reload();
										addPersonConfWindow.close();
									},
									showWaiting: true,
									failureMsg: '保存失败.'
								});
							}
	                	}
	                },{
	                    text: '重置',
	                    handler: function(){
	                		addPersonConfFormPanel.getForm().reset();
	                    }
	                }]
			    });
				// 新增人员 配置 窗口
				var addPersonConfWindow = new Ext.Window({
					title: '添加督办人员',
					width: 330,
					height: 320,
					modal: true,
					maximizable: false,
					resizable: false,
					layout:'fit',
					iconCls: 'user_add',
					plain: true,
					items: [addPersonConfFormPanel]
				}).show();
			
          })
            
            // 启用编辑 
			$("#personConfList-update-button").click(function(){
				// 启用
				submit("update");
			});

			// 删除按钮
			$("#personConfList-delete-button").click(function(){
				// 提交
				submit("delete");
			});
			
			// 排序按钮
			$("#personConfList-sort-button").click(function(){
				// 提交
				submit("sort");
			});
			
			// 上传督办人员配置信息
			$("#person-upload-button").click(function(){
				// 提交
				ReadExcel();
			});
			
			//模板下载
			$("#template-download-button").click(function(){
				//下载模板
                window.location.href = "${pageContext.request.contextPath}/alarm_vildown.action?"
			});
			
			// 提交
			function submit(type){
				var msg = "";
				var status = 0;
				if(type=="delete"){
				    status = 1;
					msg = "确定要删除该人员吗？";
				}else if(type=="update"){
				    status = 2;
				}else if(type=="sort"){
				    status = 3;
				}
				var records = personConfListGrid.getSelectionModel().getSelections();
				
				if(records.length < 1){
					 Ext.Msg.alert("提示", "请至少选择一条数据.");
				}else{
					   //如果点击的是删除
					   if(status == 1){
					      Ext.Msg.confirm("警告", msg, function(btn){
						  if(btn=="yes"){
							var personConfIds = "";
							for(var i=0;i<records.length;i++){
								personConfIds = personConfIds + records[i].get("id") + ",";
							}
							// 发送请求
							
							Anynote.ajaxRequest({
								baseUrl: '<%=baseUrl %>',
							    baseParams: {personConfIds:personConfIds },
								action: '${pageContext.request.contextPath}/deletePersonConfig.action',
								callback: function(jsonResult){
									personConfListGrid.getStore().reload();
								},
								showWaiting: true,
								failureMsg: '保存失败.'
							});
						   }
					     });
					   //如果点击的是编辑传报
					   }else if(status == 2){
					        var personConfIds = "";
						    personConfIds = records[0].get("id");
					        //只能一条一条的编辑，不能多个一起编辑
					        if(records.length == 1){
						        editPersonConfWindow = new Ext.Window({
								title: '编辑督办人员',
								width: 330,
								height: 330,
								modal: true,
								maximizable: false,
								resizable: false,
								layout:'fit',
								plain: true,
								iconCls: 'user_edit',
								autoLoad:{url:'${pageContext.request.contextPath}/page/bussmodule/perallocation/editPersonnel.jsp?personConf='+personConfIds,scripts:true,nocache:true}
								});
								editPersonConfWindow.show();
					        }else{
					            Ext.Msg.alert("提示", "只能选择一条数据.");
					        }
					   //如果点击的是督办人员排序
					   }else if(status == 3){
					        var sortIds = "";
						    sortIds = records[0].get("id");
					        //只能一条一条的排序，不能多个一起排序
					        if(records.length == 1){
							    sortPersonWindow = new Ext.Window({
							    title: '人员排序',
								width: 520,
								height: 300,
								modal: true,
				                autoScroll: true,
								maximizable: false,
								resizable: false,
								iconCls: 'hide',
								layout:'fit',
								plain: true,
								autoLoad:{url:'${pageContext.request.contextPath}/page/bussmodule/perallocation/personsort.jsp?sortIds='+sortIds,scripts:true,nocache:true}
								});
								sortPersonWindow.show();
					        }else{
					            Ext.Msg.alert("提示", "只能选择一条数据.");
					        }
					   }
					}
			}
			
			function ReadExcel()
			{
			     var dialog = new Ext.ux.UploadDialog.Dialog({
			     title: '文件上传,上传过程中请勿关闭窗口··········',
				 url: '${pageContext.request.contextPath}/uploadPersonConfig.action?deptid='+deptid,
				 //url: '${pageContext.request.contextPath}/page/bussmodule/perallocation/uploadJSPBack.jsp',
				 post_var_name:'uploadFiles',//这里是自己定义的，默认的名字叫file   
				 modal: true, 
				 reset_on_hide: false,//重置后隐藏
				 allow_close_on_upload: true,//关闭上传窗口是否仍然上传文件    
				 upload_autostart: false,//是否自动上传
				 permitted_extensions: ['xls']//允许上传的文件的类型，区分大小写   
				 });
			     dialog.show();
			     dialog.on('uploadsuccess',onUploadSuccess);//定义上传前的回调函数   
	             dialog.on('uploaderror',onUploadFailed); //定义上传出错回调函数   
	             dialog.on('fileadd',onFileAdd);//定义添加文件时验证文件的函数   
	             dialog.on('uploadcomplete',onUploadComplete);//定义所有文件上传完成回调函数
			}
			
			function onUploadSuccess(dialog, filename, resp_data, record){
			     //alert(resp_data.data);//resp_data 是返回的json数据
			     Ext.MessageBox.alert('成功提示','文件上传完成!');   
                 dialog.hide();   
			}
			
			function onUploadFailed(dialog, filename, resp_data, record){
			     Ext.MessageBox.alert('错误提示','文件上传失败!');   
			}
			
			function onFileAdd(dialog, filename){
			
			}
			
			function onUploadComplete(dialog){
			     Ext.MessageBox.alert('成功提示','文件上传完成!');   
                 //dialog.hide();   
			}
       });
 
            $("#personConf-export-button").click(function(){   //   下载 
			      var userName = Ext.getCmp("userName").getValue();
				  var regionid = <%=use.getDept_id()%>;
                  window.location.href = "${pageContext.request.contextPath}/alarm_persondown.action?region="+regionid+"&userName="+userName
		     })

	</script>
</head>
<body>
<div id="personConfListDiv">
	<div id="personConfListToolBarDiv"></div>
	<div id="personConfListGridDiv" style="width:100%;height:100%;"></div>
</div>
</body>
</html>
